/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sessionBeans;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import objects.AbilityObject;
import objects.RequestNotificationObject;
import entityBeans.Ability;
import entityBeans.RequestNotification;

/**
 *
 * @author user
 */
@Stateless
public class AbilityFacade extends Ability implements AbilityFacadeLocal {
    @PersistenceContext(unitName = "sw2")
    private EntityManager em;

    @Override
	public EntityManager getEntityManager() {
        return em;
    }

//    public AbilityFacade() {
//        super(Ability.class);
//    }

/*
 * send an empty form for adding a nem ability
 */
	@Override
	public void requestAbility(AbilityObject ability, RequestNotificationObject request) throws BeanException {
		
		if(ability!=null){
			
			if(request!=null){
				
				Ability abilityToSave= new Ability();
				abilityToSave.setAId(ability.getaId());
				abilityToSave.setDescription(ability.getdescription());
				abilityToSave.setName(ability.getName());
				abilityToSave.setAccepted("False");
				
				RequestNotification requestToSave= new RequestNotification();
				requestToSave.setCheck(request.getCheck());
				requestToSave.setNId(request.getnId());
				requestToSave.setReceiver(request.getReceiver());
				requestToSave.setType(request.getType());
				
				em.persist(abilityToSave);
				em.persist(requestToSave);
				
			}else{
				throw new BeanException("Request object is null(empty)!");
			}
			
		}else{
			throw new BeanException("Ability object is null(empty)!");
		}
	}
   
/*
 * this method will take the data from web and send a notification to admin
 */
@Override
  public void submitAbility (AbilityObject ability){
      
      }
      
/*
 * this method will read the list of abilities from the able table
 */
@Override
  public  Collection<Ability> takeListOfAbility (){
    
	Query q;
	q= em.createQuery("SELECT a FROM Ability a WHERE a.Accepted =: accepted");
	q.setParameter(accepted, "True");
	Collection<Ability> abilities= (ArrayList<Ability>) q.getResultList();
	
	return abilities;
      
	}
      
/*
 * this method will write data into  the able table
 */
 @Override
  public void addAbility (Ability ability){
      
      }

	@Override
   
	public void edit(Ability ability) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void remove(Ability ability) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Ability find(Object id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Ability> findAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Ability> findRange(int[] range) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int count() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void createAbility(Ability ability) throws BeanException {
		
		if(ability.getName()!= null){
			if(ability.getAId()==null){
				UUID id = UUID.randomUUID();
				String abilityid=id.toString();
				
				ability.setAId(abilityid);
			}
			ability.setAccepted("True");
			
			em.persist(ability);
		}else{
			throw new BeanException("Ability name can not be empty!");
		}
	}

	@Override
	public void acceptAbility(RequestNotification request) throws BeanException {
		
		Query q;
		q= em.createQuery("SELECT a FROM Ability a WHERE a.aId = :aId");
		q.setParameter(aId, request.getAbilityID());
		Ability ability= (Ability)q.getSingleResult();	
		
		ability.setAccepted("True");
		
		em.persist(ability);
	}
    
}